<template>
    <view>
        <custom><template #backText>全国邮费</template></custom>
        <list :list="data.list" empty_list_title="还没有邮费模板">
            <template #list>
                <view class="cu-list menu">
                    <nMenuItemRow2 v-for="(info, key) in data.list" :key="key" :tap="()=>{back_force_init = true}" :uri='"/seller/item/mailPrice/info?mp_id=" + info.mp_id'
                        right_icon="cuIcon-edit">
                        <template #top_title>
                            <view class="flex align-center">
                                {{info.name}}
                            </view>
                        </template>
                        <template #bottom_title>
                            <view>
                                <text>运费￥{{info.mail_price | floor}}</text>
                                <text class="margin-left-20 text-nddyny" v-if="info.free_order_price >= 0">
                                    免运费价 ￥{{info.free_order_price | floor}}
                                </text>
                            </view>
                            <view>
                                <view v-for="(province_code, key) in info.province_codes" :key="key" class="cu-btn sm bg-nddyny margin-right-4 margin-top-4">
                                    {{province_list[province_code].title}}
                                </view>
                            </view>
                        </template>
                    </nMenuItemRow2>
                </view>
            </template>
        </list>
        <foot v-if="is_insert > 0" :tap="()=>{back_force_init = true}" uri="/seller/item/mailPrice/info" 
            :title="'添加邮费模板, 剩余(' + is_insert + '), 未添加地区不能购买'">
        </foot>
    </view>
</template>

<script>
    import province_list from '../../../../components/simple-address/city-data/province2.js'
    export default {
        data() {
            return {
                back_force_init: false,
                province_list: province_list,
                is_insert: 0,
                data: {
                    offset: 0,
                    limit: 15,
                    stop: false,
                    list: [],
                },
            }
        },
        onLoad() {
            this.getList(this.$api.list_action_init);
        },
        onReachBottom() {
            this.getList(this.$api.list_action_add);
        },
        onShow() {
            if (!this.back_force_init) return;
            this.back_force_init = false;
            this.getList(this.$api.list_action_force_init);
        },
        methods: {
            getList(action) {
                this.$api.list(this, {
                    action,
                    url: '/seller/itemMailPrice/list',
                    success: (Result) => {
                        if (this.$api.form.toast(this, Result)) return;
                        this.data.list = this.data.list.concat(Result.result.list);
                        let province_codes_length = 0;
                        this.data.list.forEach(info => {
                            province_codes_length += info.province_codes.length;
                        })
                        this.is_insert = Object.keys(this.province_list).length - province_codes_length;
                    }
                });
            },
        },
    }
</script>
